package com.hunt.app.controller;


import com.hunt.dao.TransactionCalendarMapper;
import com.hunt.model.app.TransactionCalendar;
import com.hunt.model.app.nuggets.*;
import com.hunt.service.app.NuggetsService;
import com.hunt.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author: ww 热点掘金  涨跌数量 热点板块  涨跌幅排行
 * @Date : 2020/10/19
 */

@Api(value = "/app端热点掘金功能")
@Controller
@RequestMapping(path = "/app/nuggets")
public class NuggetsController {

    private static final Logger log = LoggerFactory.getLogger(NuggetsController.class);

    @Autowired
    private NuggetsService nuggetsService;

    @Resource
    private RedisTemplate<String,Object> redisTemplate;
    @Autowired
    private TransactionCalendarMapper transactionCalendarMapper;

    @ApiOperation(value = "热点掘金(涨跌数,板块排名,涨跌个股排名)", httpMethod = "GET", produces = "application/json", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "hotNuggetsIndex", method = RequestMethod.GET)
    public Result findMoreHotBlockAndStock() {
        if(isemptytime()){
            return Result.success(new ResultNuggetsVo());
        }
        else {
            ResultNuggetsVo hot_nugget = (ResultNuggetsVo)redisTemplate.boundValueOps("HOT_NUGGET").get();
            if (hot_nugget == null){
                hot_nugget = nuggetsService.findHotRank();
                //9.25之前没有柱形图
                if(isbaremptytime()){
                    hot_nugget.setFlatCount(0);
                    hot_nugget.setDownCount(0);
                    hot_nugget.setUpCount(0);
                    List<Integer> lt=new ArrayList<>();
                    for(int i=0;i<9;i++){
                        lt.add(0);
                    }
                    hot_nugget.setGroupCount(lt);
                }
                redisTemplate.boundValueOps("HOT_NUGGET").set(hot_nugget,60, TimeUnit.SECONDS);
            }
            return Result.success(hot_nugget);
        }
    }
    @ApiOperation(value = "热点掘金(涨跌数,板块排名,涨跌个股排名20个)", httpMethod = "GET", produces = "application/json", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "hotNuggetsIndexTwenty", method = RequestMethod.GET)
    public Result findMoreHotBlockAndStockTwenty() {
        if(isemptytime()){
            return Result.success(new ResultNuggetsVo());
        }
        else {
            ResultNuggetsVo hot_nugget = (ResultNuggetsVo)redisTemplate.boundValueOps("HOT_NUGGET_TWENTY").get();
            if (hot_nugget == null){
                hot_nugget = nuggetsService.findHotRankTwenty();
                //9.25之前没有柱形图
                if(isbaremptytime()){
                    hot_nugget.setFlatCount(0);
                    hot_nugget.setDownCount(0);
                    hot_nugget.setUpCount(0);
                    List<Integer> lt=new ArrayList<>();
                    for(int i=0;i<9;i++){
                        lt.add(0);
                    }
                    hot_nugget.setGroupCount(lt);
                }
                redisTemplate.boundValueOps("HOT_NUGGET_TWENTY").set(hot_nugget,60, TimeUnit.SECONDS);
            }
            return Result.success(hot_nugget);
        }
    }
    @ApiOperation(value = "热点掘金(根据不同类型查询涨跌数)", httpMethod = "GET", produces = "application/json", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "findUpsDownsByIndexType", method = RequestMethod.GET)
    public Result findUpsDownsByIndexType(@RequestParam(value = "indexType",required = true) int indexType) {
        if(isemptytime()){
            return Result.success(new ResultNuggetsVo());
        }
        else {
            ResultNuggetsVo hot_nugget = null;
            if(indexType == 0){
                hot_nugget = (ResultNuggetsVo)redisTemplate.boundValueOps("HOT_NUGGET_INDEXTYPE0").get();
            }
            else if (indexType == 1){
                hot_nugget = (ResultNuggetsVo)redisTemplate.boundValueOps("HOT_NUGGET_INDEXTYPE1").get();
            }
            else if (indexType == 2){
                hot_nugget = (ResultNuggetsVo)redisTemplate.boundValueOps("HOT_NUGGET_INDEXTYPE2").get();
            }
            if (hot_nugget == null){
                hot_nugget = nuggetsService.findUpsDownsByIndexType(indexType);
                //9.25之前没有柱形图
                if(isbaremptytime()){
                    hot_nugget.setFlatCount(0);
                    hot_nugget.setDownCount(0);
                    hot_nugget.setUpCount(0);
                }
                if(indexType == 0){
                    redisTemplate.boundValueOps("HOT_NUGGET_INDEXTYPE0").set(hot_nugget,60, TimeUnit.SECONDS);
                }
                else if (indexType == 1){
                    redisTemplate.boundValueOps("HOT_NUGGET_INDEXTYPE1").set(hot_nugget,60, TimeUnit.SECONDS);
                }
                else if (indexType == 2){
                    redisTemplate.boundValueOps("HOT_NUGGET_INDEXTYPE2").set(hot_nugget,60, TimeUnit.SECONDS);
                }
            }
            return Result.success(hot_nugget);
        }
    }

    @ApiOperation(value = "热点掘金(查看更多的板块排行,根据传入参数正序倒叙)", httpMethod = "GET", produces = "application/json", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "findMoreBlockBySort", method = RequestMethod.GET)
    public Result findMoreBlockSort(@RequestParam(value = "sort",defaultValue = "1",required = false) int sort,
                                    @RequestParam(value = "point",defaultValue = "0",required = false) int point,
                                    @RequestParam(value = "limit",defaultValue = "15",required = false) int limit) {
        if(isemptytime()){
            List<BlockShow> moreBlockSort =new ArrayList<>();
            return Result.success( moreBlockSort);
        }
        else {
            List<BlockShow> moreBlockSort = nuggetsService.findMoreBlockSort(sort,point,point + limit);
            return Result.success(moreBlockSort);
        }

    }

    @ApiOperation(value = "热点掘金(查看板块内股票涨跌排行,根据传入参数正序倒叙)", httpMethod = "GET", produces = "application/json", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "findStockListWithinBlock", method = RequestMethod.GET)
    public Result findStockListWithinBlock(@RequestParam(value = "blockCode") String blockCode,
                                               @RequestParam(value = "sort",defaultValue = "1",required = false) int sort,
                                               @RequestParam(value = "point",defaultValue = "0",required = false) int point,
                                               @RequestParam(value = "limit",defaultValue = "15",required = false) int limit) {
        if(isemptytime()){
            BlockInfo stockListWithinBlock =new BlockInfo();
            return Result.success(stockListWithinBlock);
        }
        else {
        BlockInfo stockListWithinBlock = nuggetsService.findStockListWithinBlock(blockCode, sort, point,point + limit);
        return Result.success(stockListWithinBlock);}
    }

    @ApiOperation(value = "热点掘金(查看涨幅排行,根据传入参数正序倒叙)", httpMethod = "GET", produces = "application/json", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "findHlPercentRankList", method = RequestMethod.GET)
    public Result findHlPercentRankList(@RequestParam(value = "sort", defaultValue = "1",required = false) int sort,
                                        @RequestParam(value = "position", defaultValue = "0",required = false) int position,
                                        @RequestParam(value = "size", defaultValue = "15",required = false) int size,
                                        @RequestParam(value = "type",defaultValue = "0",required = false) int type) {
        if(isemptytime()){
            List<BlockStock> hlPercentRankList=new ArrayList<>();
            return Result.success(hlPercentRankList);
        }
        else {
        List<BlockStock> hlPercentRankList = nuggetsService.findHlPercentRankList(sort, position, position + size,type);
        return Result.success(hlPercentRankList);}
    }
    private  boolean isemptytime(){
        Date date=new Date();
        SimpleDateFormat s=new SimpleDateFormat("yyyyMMdd");
        LocalTime now = LocalTime.now();
        TransactionCalendar transactionCalendar=transactionCalendarMapper.findByCalDate(s.format(date));
        boolean istransaction=transactionCalendar.getIs_open().equals("1");
        boolean istruetime= now.isAfter(LocalTime.of(9, 0, 0)) && now.isBefore(LocalTime.of(9, 14, 0));
        return istransaction&&istruetime;
    }
    private  boolean isbaremptytime(){
        Date date=new Date();
        SimpleDateFormat s=new SimpleDateFormat("yyyyMMdd");
        LocalTime now = LocalTime.now();
        TransactionCalendar transactionCalendar=transactionCalendarMapper.findByCalDate(s.format(date));
        boolean istransaction=transactionCalendar.getIs_open().equals("1");
        boolean istruetime= now.isAfter(LocalTime.of(9, 0, 0)) && now.isBefore(LocalTime.of(9, 25, 0));
        return istransaction&&istruetime;
    }
}
